package com.meida.module.bus.provider.service.impl;

import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.mybatis.query.CriteriaUpdate;
import com.meida.common.utils.FlymeUtils;
import com.meida.common.utils.JsonUtils;
import com.meida.module.bus.client.entity.BusProject;
import com.meida.module.bus.client.entity.BusProjectSupplier;
import com.meida.module.bus.client.entity.BusSupplier;
import com.meida.module.bus.client.entity.BusVersion;
import com.meida.module.bus.provider.mapper.BusProjectSupplierMapper;
import com.meida.module.bus.provider.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 项目分包商接口实现类
 *
 * @author flyme
 * @date 2020-10-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusProjectSupplierServiceImpl extends BaseServiceImpl<BusProjectSupplierMapper, BusProjectSupplier> implements BusProjectSupplierService {
    @Autowired
    private BusSupplierService busSupplierService;

    @Autowired
    private BusVersionService busVersionService;

    @Autowired
    private BusMaterialPackage100Service busMaterialPackage100Service;

    @Autowired
    private BusMaterialPackage200Service busMaterialPackage200Service;

    @Autowired
    private BusReportService busReportService;

    @Autowired
    private BusProjectService busProjectService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusProjectSupplier bps, EntityMap extra) {
        return ResultBody.ok();
    }

    @Override
    public ResultBody beforeEdit(CriteriaUpdate<BusProjectSupplier> cu, BusProjectSupplier busProjectSupplier, EntityMap extra) {
        Long projectId = busProjectSupplier.getProjectId();
        BusProject busProject = busProjectService.getById(projectId);
        Integer state = FlymeUtils.getInteger(busProject.getState(), 0);
        if (state == 1) {
            return ResultBody.failed("项目已锁定");
        }
        Long projectSupplierId = busProjectSupplier.getProjectSupplierId();
        BusProjectSupplier oldProjectSupplier = getById(projectSupplierId);
        Map<Object, Object> map = new HashMap<>();
        map.put("projectId", projectId);
        map.put("majorCode", oldProjectSupplier.getMajorCode());
        map.put("packageNo", oldProjectSupplier.getPackageNo());
        EntityMap allSupplierNum = busMaterialPackage100Service.getAllSupplierNum(map);

        if (FlymeUtils.isNotEmpty(oldProjectSupplier)) {
            EntityMap requestMap = cu.getRequestMap();
            Long oldSupplierId1 = oldProjectSupplier.getSupplierId1();
            Long newSupplierId1 = requestMap.getLong("supplierId1");
            if (FlymeUtils.isNotEmpty(newSupplierId1) && FlymeUtils.isNotEmpty(oldSupplierId1)) {
                if (newSupplierId1.longValue() != oldSupplierId1.longValue()) {
                    BigDecimal totalSupplierNum1 = allSupplierNum.getBigDecimal("totalSupplierNum1");
                    if (FlymeUtils.gt(totalSupplierNum1, BigDecimal.ZERO)) {
                        return ResultBody.failed("包商1已经填写过包商量");
                    }
                }
            }
            Long oldSupplierId2 = oldProjectSupplier.getSupplierId2();
            Long newSupplierId2 = requestMap.getLong("supplierId2");
            if (FlymeUtils.isNotEmpty(newSupplierId2) && FlymeUtils.isNotEmpty(oldSupplierId2)) {
                if (newSupplierId2.longValue() != oldSupplierId2.longValue()) {
                    BigDecimal totalSupplierNum2 = allSupplierNum.getBigDecimal("totalSupplierNum2");
                    if (FlymeUtils.gt(totalSupplierNum2, BigDecimal.ZERO)) {
                        return ResultBody.failed("包商2已经填写过包商量");
                    }
                }
            }
            Long oldSupplierId3 = oldProjectSupplier.getSupplierId3();
            Long newSupplierId3 = requestMap.getLong("supplierId3");
            if (FlymeUtils.isNotEmpty(newSupplierId3) && FlymeUtils.isNotEmpty(oldSupplierId3)) {
                if (newSupplierId3.longValue() != oldSupplierId3.longValue()) {
                    BigDecimal totalSupplierNum3 = allSupplierNum.getBigDecimal("totalSupplierNum3");
                    if (FlymeUtils.gt(totalSupplierNum3, BigDecimal.ZERO)) {
                        return ResultBody.failed("包商3已经填写过包商量");
                    }
                }
            }
            Long oldSupplierId4 = oldProjectSupplier.getSupplierId4();
            Long newSupplierId4 = requestMap.getLong("supplierId4");
            if (FlymeUtils.isNotEmpty(oldSupplierId4) && FlymeUtils.isNotEmpty(newSupplierId4)) {
                if (newSupplierId4.longValue() != oldSupplierId4.longValue()) {
                    BigDecimal totalSupplierNum4 = allSupplierNum.getBigDecimal("totalSupplierNum4");
                    if (FlymeUtils.gt(totalSupplierNum4, BigDecimal.ZERO)) {
                        return ResultBody.failed("包商4已经填写过包商量");
                    }
                }
            }
            Long oldSupplierId5 = oldProjectSupplier.getSupplierId5();
            Long newSupplierId5 = requestMap.getLong("supplierId5");
            if (FlymeUtils.isNotEmpty(oldSupplierId5) && FlymeUtils.isNotEmpty(newSupplierId5)) {
                if (newSupplierId5.longValue() != oldSupplierId5.longValue()) {
                    BigDecimal totalSupplierNum5 = allSupplierNum.getBigDecimal("totalSupplierNum5");
                    if (FlymeUtils.gt(totalSupplierNum5, BigDecimal.ZERO)) {
                        return ResultBody.failed("包商5已经填写过包商量");
                    }
                }
            }
            Long oldSupplierId6 = oldProjectSupplier.getSupplierId6();
            Long newSupplierId6 = requestMap.getLong("supplierId6");
            if (FlymeUtils.isNotEmpty(oldSupplierId6) && FlymeUtils.isNotEmpty(newSupplierId6)) {
                if (newSupplierId6.longValue() != oldSupplierId6.longValue()) {
                    BigDecimal totalSupplierNum6 = allSupplierNum.getBigDecimal("totalSupplierNum6");
                    if (FlymeUtils.gt(totalSupplierNum6, BigDecimal.ZERO)) {
                        return ResultBody.failed("包商6已经填写过包商量");
                    }
                }
            }
            Long oldSupplierId7 = oldProjectSupplier.getSupplierId7();
            Long newSupplierId7 = requestMap.getLong("supplierId7");
            if (FlymeUtils.isNotEmpty(oldSupplierId7) && FlymeUtils.isNotEmpty(newSupplierId7)) {
                if (newSupplierId3.longValue() != oldSupplierId3.longValue()) {
                    BigDecimal totalSupplierNum7 = allSupplierNum.getBigDecimal("totalSupplierNum7");
                    if (FlymeUtils.gt(totalSupplierNum7, BigDecimal.ZERO)) {
                        return ResultBody.failed("包商7已经填写过包商量");
                    }
                }
            }
            Long oldSupplierId8 = oldProjectSupplier.getSupplierId8();
            Long newSupplierId8 = requestMap.getLong("supplierId8");
            if (FlymeUtils.isNotEmpty(oldSupplierId8) && FlymeUtils.isNotEmpty(newSupplierId8)) {
                if (newSupplierId8.longValue() != oldSupplierId8.longValue()) {
                    BigDecimal totalSupplierNum8 = allSupplierNum.getBigDecimal("totalSupplierNum8");
                    if (FlymeUtils.gt(totalSupplierNum8, BigDecimal.ZERO)) {
                        return ResultBody.failed("包商8已经填写过包商量");
                    }
                }
            }
            Long oldSupplierId9 = oldProjectSupplier.getSupplierId9();
            Long newSupplierId9 = requestMap.getLong("supplierId9");
            if (FlymeUtils.isNotEmpty(oldSupplierId9) && FlymeUtils.isNotEmpty(newSupplierId9)) {
                if (newSupplierId9.longValue() != oldSupplierId9.longValue()) {
                    BigDecimal totalSupplierNum9 = allSupplierNum.getBigDecimal("totalSupplierNum9");
                    if (FlymeUtils.gt(totalSupplierNum9, BigDecimal.ZERO)) {
                        return ResultBody.failed("包商9已经填写过包商量");
                    }
                }
            }
            Long oldSupplierId10 = oldProjectSupplier.getSupplierId10();
            Long newSupplierId10 = requestMap.getLong("supplierId10");
            if (FlymeUtils.isNotEmpty(oldSupplierId10) && FlymeUtils.isNotEmpty(newSupplierId10)) {
                if (newSupplierId10.longValue() != oldSupplierId10.longValue()) {
                    BigDecimal totalSupplierNum10 = allSupplierNum.getBigDecimal("totalSupplierNum10");
                    if (FlymeUtils.gt(totalSupplierNum10, BigDecimal.ZERO)) {
                        return ResultBody.failed("包商10已经填写过包商量");
                    }
                }
            }
        }

        return super.beforeEdit(cu, busProjectSupplier, extra);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusProjectSupplier> cq, BusProjectSupplier bps, EntityMap requestMap) {
        cq.orderByDesc("bps.createTime");
        return ResultBody.ok();
    }

    @Override
    public EntityMap selectBusProjectSupplier(Map params) {
        CriteriaQuery cq = new CriteriaQuery(BusProjectSupplier.class);
        cq.eq("projectId", params.get("projectId"));
        cq.eq("majorCode", params.get("majorCode"));
        cq.eq("packageNo", params.get("packageNo"));
        cq.last("LIMIT 1");
        return findOne(cq);
    }

    @Override
    public List<EntityMap> getBusProjectAllSupplier(Map params) {
        EntityMap entityMap = selectBusProjectSupplier(params);
        Long versionId = FlymeUtils.getLong(params.get("versionId"));
        BusVersion busVersion = busVersionService.getById(versionId);
        EntityMap busMaterialPackageInfo = null;
        if (FlymeUtils.isNotEmpty(busVersion)) {
            Integer bigVersion = FlymeUtils.getInteger(busVersion.getBigVersion(), 0);
            int version = bigVersion / 100;
            Long materialPackage100Id = FlymeUtils.getLong(params.get("materialPackage100Id"));
            if (version == 1) {
                busMaterialPackageInfo = busMaterialPackage100Service.findById(materialPackage100Id);
            }
            if (version == 2) {
                busMaterialPackageInfo = busMaterialPackage200Service.findById(materialPackage100Id);
            }
        }
        List<EntityMap> supplierList = new ArrayList<>();
        Long supplierId1 = TypeUtils.castToLong(entityMap.get("supplierId1"));
        supplierList = initSupplierEntityMap(supplierList, busMaterialPackageInfo, supplierId1, params, 1);
        Long supplierId2 = TypeUtils.castToLong(entityMap.get("supplierId2"));
        supplierList = initSupplierEntityMap(supplierList, busMaterialPackageInfo, supplierId2, params, 2);
        Long supplierId3 = entityMap.get("supplierId3");
        supplierList = initSupplierEntityMap(supplierList, busMaterialPackageInfo, supplierId3, params, 3);
        Long supplierId4 = entityMap.get("supplierId4");
        supplierList = initSupplierEntityMap(supplierList, busMaterialPackageInfo, supplierId4, params, 4);
        Long supplierId5 = entityMap.get("supplierId5");
        supplierList = initSupplierEntityMap(supplierList, busMaterialPackageInfo, supplierId5, params, 5);
        Long supplierId6 = entityMap.get("supplierId6");
        supplierList = initSupplierEntityMap(supplierList, busMaterialPackageInfo, supplierId6, params, 6);
        Long supplierId7 = entityMap.get("supplierId7");
        supplierList = initSupplierEntityMap(supplierList, busMaterialPackageInfo, supplierId7, params, 7);
        Long supplierId8 = entityMap.get("supplierId8");
        supplierList = initSupplierEntityMap(supplierList, busMaterialPackageInfo, supplierId8, params, 8);
        Long supplierId9 = entityMap.get("supplierId9");
        supplierList = initSupplierEntityMap(supplierList, busMaterialPackageInfo, supplierId9, params, 9);
        Long supplierId10 = entityMap.get("supplierId10");
        supplierList = initSupplierEntityMap(supplierList, busMaterialPackageInfo, supplierId10, params, 10);
        return supplierList;
    }

    @Override
    public List<EntityMap> getBusProjectSupplier(Map params) {
        List<EntityMap> busProjectSupplierIds = getBusProjectSupplierIds(params);
        List<Long> supplierIds = busProjectSupplierIds.stream().map(entityMap -> entityMap.getLong("supplierId")).collect(Collectors.toList());
        Collection<BusSupplier> busSupplierList = Lists.newArrayList();
        if (supplierIds.size() > 0) {
            busSupplierList = busSupplierService.listByIds(supplierIds);
            for (EntityMap entityMap : busProjectSupplierIds) {
                Long supplierId = entityMap.getLong("supplierId");
                for (BusSupplier busSupplier : busSupplierList) {
                    Long supplierId1 = busSupplier.getSupplierId();
                    if(supplierId.longValue()==supplierId1.longValue()){
                        entityMap.putAll( JsonUtils.beanToMap(busSupplier));
                    }
                }
            }
        }
        return busProjectSupplierIds;
    }

    @Override
    public List<EntityMap> getSupplierList(Map params) {
        List<EntityMap> supplierList = getBusProjectAllSupplier(params);
        List<Long> supplierIds = new ArrayList<>();
        for (EntityMap entityMap : supplierList) {
            Long supplierId = entityMap.getLong("supplierId");
            supplierIds.add(supplierId);
        }
        if (supplierIds.size() > 0) {
            List<BusSupplier> busSupplierList = (List<BusSupplier>) busSupplierService.listByIds(supplierIds);
            for (EntityMap entityMap : supplierList) {
                Long supplierId = entityMap.getLong("supplierId");
                for (BusSupplier busSupplier : busSupplierList) {
                    Long supplierId1 = busSupplier.getSupplierId();
                    if (supplierId.longValue() == supplierId1.longValue()) {
                        String supplierName = busSupplier.getSupplierName();
                        entityMap.put("supplierName", supplierName);
                        break;
                    }
                }
            }
        }
        return supplierList;
    }

    @Override
    public List<EntityMap> getBusProjectSupplierIds(Map params) {
        EntityMap projectSupplierMap = selectBusProjectSupplier(params);
        Long supplierId1 = projectSupplierMap.getLong("supplierId1");
        Long supplierId2 = projectSupplierMap.getLong("supplierId2");
        Long supplierId3 = projectSupplierMap.getLong("supplierId3");
        Long supplierId4 = projectSupplierMap.getLong("supplierId4");
        Long supplierId5 = projectSupplierMap.getLong("supplierId5");
        Long supplierId6 = projectSupplierMap.getLong("supplierId6");
        Long supplierId7 = projectSupplierMap.getLong("supplierId7");
        Long supplierId8 = projectSupplierMap.getLong("supplierId8");
        Long supplierId9 = projectSupplierMap.getLong("supplierId9");
        Long supplierId10 = projectSupplierMap.getLong("supplierId10");
        List<EntityMap> supplierIdList = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(supplierId1)) {
            EntityMap entityMap = new EntityMap();
            entityMap.put("index", 1);
            entityMap.put("supplierId", supplierId1);
            supplierIdList.add(entityMap);
        }
        if (FlymeUtils.isNotEmpty(supplierId2)) {
            EntityMap entityMap = new EntityMap();
            entityMap.put("index", 2);
            entityMap.put("supplierId", supplierId2);
            supplierIdList.add(entityMap);
        }
        if (FlymeUtils.isNotEmpty(supplierId3)) {
            EntityMap entityMap = new EntityMap();
            entityMap.put("index", 3);
            entityMap.put("supplierId", supplierId3);
            supplierIdList.add(entityMap);
        }
        if (FlymeUtils.isNotEmpty(supplierId4)) {
            EntityMap entityMap = new EntityMap();
            entityMap.put("index", 4);
            entityMap.put("supplierId", supplierId4);
            supplierIdList.add(entityMap);
        }
        if (FlymeUtils.isNotEmpty(supplierId5)) {
            EntityMap entityMap = new EntityMap();
            entityMap.put("index", 5);
            entityMap.put("supplierId", supplierId5);
            supplierIdList.add(entityMap);
        }
        if (FlymeUtils.isNotEmpty(supplierId6)) {
            EntityMap entityMap = new EntityMap();
            entityMap.put("index", 6);
            entityMap.put("supplierId", supplierId6);
            supplierIdList.add(entityMap);
        }
        if (FlymeUtils.isNotEmpty(supplierId7)) {
            EntityMap entityMap = new EntityMap();
            entityMap.put("index", 7);
            entityMap.put("supplierId", supplierId7);
            supplierIdList.add(entityMap);
        }
        if (FlymeUtils.isNotEmpty(supplierId8)) {
            EntityMap entityMap = new EntityMap();
            entityMap.put("index", 8);
            entityMap.put("supplierId", supplierId8);
            supplierIdList.add(entityMap);
        }
        if (FlymeUtils.isNotEmpty(supplierId9)) {
            EntityMap entityMap = new EntityMap();
            entityMap.put("index", 9);
            entityMap.put("supplierId", supplierId9);
            supplierIdList.add(entityMap);
        }
        if (FlymeUtils.isNotEmpty(supplierId10)) {
            EntityMap entityMap = new EntityMap();
            entityMap.put("index", 10);
            entityMap.put("supplierId", supplierId10);
            supplierIdList.add(entityMap);
        }
        return supplierIdList;
    }

    private List<EntityMap> initSupplierEntityMap(List<EntityMap> supplierList, EntityMap busMaterialPackageInfo, Long supplierId, Map params, int numNo) {
        Long projectId = FlymeUtils.getLong(params.get("projectId"));
        Long materialId = FlymeUtils.getLong(params.get("materialId"));
        String majorCode = TypeUtils.castToString(params.get("majorCode"));
        String packageNo = TypeUtils.castToString(params.get("packageNo"));
        EntityMap sumHaveReport = busReportService.sumHaveReportSupplierNum(projectId, majorCode, packageNo, supplierId, materialId);
        if (FlymeUtils.isNotEmpty(supplierId)) {
            EntityMap entityMap = new EntityMap();
            entityMap.put("supplierId", supplierId);
            if (FlymeUtils.isNotEmpty(busMaterialPackageInfo)) {
                BigDecimal bigDecimal = busMaterialPackageInfo.getBigDecimal("supplierNum" + numNo);
                entityMap.put("supplierNum", "合同量" + bigDecimal);
                BigDecimal haveTotalDeclareQuantity = sumHaveReport.getBigDecimal("totalDeclareQuantity");
                BigDecimal restSupplierNum = bigDecimal.subtract(haveTotalDeclareQuantity);
                entityMap.put("restSupplierNum", "剩余合同量" + restSupplierNum);
            } else {
                entityMap.put("supplierNum", "合同量" + 0);
                entityMap.put("restSupplierNum", "剩余合同量" + 0);
            }
            //控制前端分包商显示，作为标识
            entityMap.put("index", numNo);
            supplierList.add(entityMap);
        }
        return supplierList;
    }

}
